package com.amuse.amuse.controller;

import com.amuse.amuse.entity.ResponseData;
import com.amuse.amuse.entity.ResultCode;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Controller
public class BaseController {
    public ResponseData resultBoolean(Boolean data) {
        ResponseData responseData = new ResponseData() ;
        if(null == data || ! data) {   // 失败
            responseData.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            responseData.setMsg(ResultCode.COMMON_FAIL.getMessage());
        } else {
            responseData.setErrorCode(ResultCode.SUCCESS.getCode());
            responseData.setMsg(ResultCode.SUCCESS.getMessage());
        }
        responseData.setData(data);
        return  responseData ;
    }
    /**
     * service层返回数据是对象或者数组类型，可以调用该方法生产ResponseData对象
     */
    public ResponseData resultObj(Object data) {
        ResponseData responseData = new ResponseData() ;
        if(null == data) {   //  失败
            responseData.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            responseData.setMsg(ResultCode.COMMON_FAIL.getMessage());
        } else {
            responseData.setErrorCode(ResultCode.SUCCESS.getCode());
            responseData.setMsg(ResultCode.SUCCESS.getMessage());
            responseData.setData(data);
        }
        return  responseData ;
    }
    /**
     * service层返回数据是Page<T></T>类型，可以调用该方法生产ResponseData对象
     */
    public Map<String , Object> resultPage(boolean isOk , long count , Object data) {
        Map<String , Object> map = new HashMap<>() ;
        if(isOk) {
            map.put("code" , 0) ;
            map.put("msg" , "请求成功!") ;
            map.put("count" , count) ;
            map.put("data" , data) ;
        } else {
            map.put("code" , -1) ;
            map.put("msg" , "请求失败!") ;
            map.put("count" , 0) ;
            map.put("data" , null ) ;
        }
        return map ;
    }

}
